package com.LeeCode;

import java.util.Arrays;


/**
 * 数组的最大美丽值
 */

public class Code2779 {
    public static void main(String[] args) {
        int[] nums = {4,6,1,2};
        int k = 2;
        System.out.println(new Code2779().maximumBeauty(nums, k));
    }

    public int maximumBeauty(int[] nums, int k) {
        Arrays.sort(nums);
        int ans = 0, r = 0, len = k * 2;
        for (int i = 0; i < nums.length; i++) {
            int start = nums[i];
            int end = start + len;

            while (r < nums.length && nums[r] <= end) {
                r++;
            }
            ans = Math.max(ans, r - i);
        }

        return ans;
    }
}
